package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebRoleData;
import com.file.system.mapper.system.WebRoleDataMapper;
import com.file.system.service.system.IWebRoleDataService;
import com.file.system.tools.page.DP;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 角色数据权限 实现
 *
 * @author jch
 * @since 2022-08-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebRoleDataServiceImpl implements IWebRoleDataService {

    @Autowired
    private WebRoleDataMapper mapper;


    //添加角色数据权限
    public boolean save(WebRoleData webRoleData) {
        webRoleData.setId(IdUtil.getSnowflakeNextId());
        return SqlHelper.retBool(mapper.save(webRoleData));
    }


    //删除角色数据权限
    public boolean removeById(@Param("id") Long id) {
        return SqlHelper.retBool(mapper.removeById(new WebRoleData().setId(id)));
    }

    //编辑角色数据权限
    public boolean updateById(@Param("et") WebRoleData webRoleData) {
        if (ObjectUtil.isEmpty(webRoleData.getId())) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webRoleData));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebRoleData> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加角色数据权限(批量)
    public boolean saveBatch(@Param("list") List<WebRoleData> webRoleDataList) {
        if (webRoleDataList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webRoleDataList));
    }

    //查询角色数据权限
    @DS("slave")
    public List<WebRoleData> list(@Param("ew") QueryWrapper<WebRoleData> query) {
        return mapper.select(query);
    }

    //查询角色数据权限分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebRoleData> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebRoleData> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebRoleData> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询角色数据权限
    @DS("slave")
    public WebRoleData getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询角色数据权限
    @DS("slave")
    public WebRoleData getOne(@Param("ew") QueryWrapper<WebRoleData> query) {
        List<WebRoleData> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询角色数据权限数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebRoleData> query) {
        return mapper.selectCount(query);
    }
}
